import Vue from 'vue'
import Vuex from 'vuex'
import axios from '../../node_modules/axios/dist/axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    navIndex: 0,
    veriCode: '',
    needVeriCode: false,
    userName: ''
  },
  mutations: {
    selectNav (state, index) {
      state.navIndex = index
    },
    setUserName (state, userName) {
      state.userName = userName
    },
    obtainVeriCode (state, veriCodeInfo) {
      state.veriCode = veriCodeInfo[0]
      state.needVeriCode = veriCodeInfo[1]
    },
    clearVeriCode (state) {
      state.veriCode = ''
      state.needVeriCode = false
    }
  },
  actions: {
    obtainVeriCodeAsyn ({ commit }, operator) {
      axios.get('/login/getVeriCode', {params: operator}).then(function (res) {
        if (res.data.status === 200) {
          if (res.data.datas.needVeriCode) {
            let veriCode = res.data.datas.veriCode
            commit('obtainVeriCode', [veriCode, res.data.datas.needVeriCode])
          }
        }
      })
    }
  }
})
